package com.app.view;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import phone.qyg.com.qyg.R;

/**
 * 作者：lblbh on 2017/9/25 14:35
 * 邮箱：lanbuhan@163.com
 * 功能：第一次引导页
 */
public class AdvertView extends android.support.v4.view.ViewPager{

    private List<Integer> drawables;

    private OnTransformController onTransformController;

    private Context context;

    private Class clazz;

    private int layouId;

    List<View> views;

    public AdvertView(Context context) {
        super(context);
    }

    public AdvertView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AdvertView(Context context, Class clazz) {
        super(context);
        this.clazz = clazz;
    }

    public AdvertView(Context context, AttributeSet attrs, Class clazz) {
        super(context, attrs);
        this.clazz = clazz;
    }

    public AdvertView(Context context, int layouId, List<Integer> drawables, OnTransformController onTransformController){
        super(context);
        this.onTransformController = onTransformController;
        this.drawables = drawables;
        this.context = context;
        this.layouId = layouId;
        initView();
    }

    private void initView(){
        if(layouId <=0){
            throw new IllegalArgumentException("empty view layoutid,please add View id");
        }

        if(!CommonUtil.isRequestList(drawables)){
            throw  new IllegalArgumentException("cureent drawable list empty");
        }

        views = new ArrayList<View>();
        for (int i=0,j=drawables.size();i<j;i++){
            View itemView = View.inflate(context,layouId,null);
            itemView.setBackgroundResource(drawables.get(i));
            views.add(itemView);
        }

        this.setAdapter(pagerAdapter);
    }

    PagerAdapter pagerAdapter = new PagerAdapter() {

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            container.removeView(views.get(position));
        }

        @Override
        public int getItemPosition(Object object) {

            return super.getItemPosition(object);
        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = views.get(position);
            container.addView(view);
            TextView btn=(TextView) view.findViewById(R.id.btn);
            btn.setVisibility((position == (views.size()-1)?View.VISIBLE:View.GONE));
            if(position == (views.size()-1)){
                btn.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
//                        TransformController.transformController(QXApplication.getContext(),MainActivity.class);
                        if(onTransformController != null)
                            onTransformController.onTransformController();
                    }
                });
            }

            return view;
        }

    };


    public interface OnTransformController{
        public void onTransformController();
    }
}
